//
//  GoodCommentCell.m
//  B2C
//
//  Created by xiaowu on 2017/7/31.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "GoodCommentCell.h"

@interface GoodCommentCell ()

@end

@implementation GoodCommentCell

+ (CGFloat)getHeightWithContent:(NSString *)content withImages:(NSArray *)imgArrs {
    CGFloat height = 0 ;
    height += 15;
    height += 40;
    height += 10;
    // content
    height += [GoodCommentCell getContentHeightWithContent:content];
    // image
    height += [ShowPicturesCell getHeightWithArr:imgArrs];
    height += 15;
    return height;
}

+ (CGFloat)getContentHeightWithContent:(NSString *)str {
    CGFloat height = 0;
    if (str.length == 0) {
        str = @"此用户没有填写评价。";
    }
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range = NSMakeRange(0, str.length);
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackTextColor] range:range];
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:range];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 6;
    style.alignment = NSTextAlignmentJustified;
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:style range:range];
    
    CGRect paragraphRect =
    [attributedStr boundingRectWithSize:CGSizeMake(ScreenWidth - 20, CGFLOAT_MAX)
                                options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                context:nil];
    height += ceilf(paragraphRect.size.height);
    return height;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.backgroundColor = [UIColor whiteColor];
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.picturesCell = [[ShowPicturesCell alloc] init];
    [self.contentView addSubview:self.picturesCell.contentView];
    [self.contentView addSubview:self.heartView];
    
    self.headImageView.layer.cornerRadius = self.headImageView.height * 0.5;
    self.headImageView.clipsToBounds = YES;
    self.nickNameLabel.font = TextNormalFont14;
    self.nickNameLabel.textColor = [UIColor blackTextColor];
    self.timeLabel.font = TextNormalFont12;
    self.timeLabel.textColor = [UIColor gray66Color];
    self.headImageView.backgroundColor = [UIColor gray66Color];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setValueForCellWithContent:(NSString *)str
                           headUrl:(NSString *)headUrl
                              time:(NSString *)time
                         nickNamek:(NSString *)nickName
                             point:(NSString *)point
                        withImages:(NSArray *)imgArrs
                       isAnonymous:(BOOL)isAnonymous {
    if (str.length == 0) {
        str = @"此用户没有填写评价。";
    }
    // attribute  赋值
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
    NSRange range = NSMakeRange(0, str.length);
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor blackTextColor] range:range];
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:16] range:range];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 6;
    style.alignment = NSTextAlignmentJustified;
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:style range:range];
    self.contentLabel.attributedText = attributedStr;
    
    
    self.contentLabel.height = [GoodCommentCell getContentHeightWithContent:str];
    
    [self.picturesCell setImageShowStyleWithArr:imgArrs];
    self.picturesCell.contentView.height = [ShowPicturesCell getHeightWithArr:imgArrs];

    [self.heartView setImageViewWithPoint:point.floatValue withType:HeartTypeForLittle];
    if (isAnonymous) {
        nickName = @"匿名用户";
        headUrl = @"";
    }
    self.nickNameLabel.text = nickName;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:headUrl] placeholderImage:PlaceholderImageHeader];
    self.timeLabel.text = [DateSomeThing getDateStrWithTimestampstr:time formatStr:@"yyyy-MM-dd HH:mm"];
    [self layoutSubviews];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    self.picturesCell.contentView.frame = CGRectMake(0, self.contentLabel.bottom, self.contentView.width, self.picturesCell.contentView.height);
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.picturesCell.contentView.frame = CGRectMake(0, self.contentLabel.bottom, self.contentView.width, self.picturesCell.contentView.height);
    
    self.heartView.frame = CGRectMake(self.contentView.width - 10 - [HeartFiveView getFiveHeartViewSizeWithPoint:5.0 withType:HeartTypeForLittle].width, self.headImageView.top, [HeartFiveView getFiveHeartViewSizeWithPoint:5.0 withType:HeartTypeForLittle].width, [HeartFiveView getFiveHeartViewSizeWithPoint:5.0 withType:HeartTypeForLittle].height);
}

- (HeartFiveView *)heartView {
    if (!_heartView) {
        _heartView = [[HeartFiveView alloc] init];
    }
    return _heartView;
}

@end
